#coding:utf8
from config import *
from config_mouse_start import *
from routin_x2 import *
from routin_base import *                  

def x1_kuai():
    if cfg.x2_mode:
        if cfg.x2_ini_mouse==1:
            cfg.x2_ini_mouse=2
        else:
            cfg.x2_ini_mouse=1
    else:
        cfg.no_left_move = not cfg.no_left_move
        
    set_cfg_sum_0()    

def x2_kuai():
    switch_showcut()

def x3_kuai():
    cfg.cmd_mode = True

def add_kuai():
    if cfg.w2showing:
        cfg.sum='6'
        xing()        
        set_cfg_sum_0()
        return
    set_to_last(True)
    set_cfg_sum_0()

def enter_kuai():
    switch_showcut()
    return

def enter_kuai2():
    if cfg.w2showing:
        ctrl2()
    return

def xing_kuai():
    if not cfg.w2showing:
        set_to_last(False)    
    else:
        cfg.sum='6.'
        xing()   
    set_cfg_sum_0()

def xing_kuai2():
    dotdot()
    set_cfg_sum_0()

def minu_kuai():
    show_clear()    
    switch_show()
    set_cfg_sum_0()

def ctrl_cap():
    cfg.ctrlcap = not cfg.ctrlcap
    return
    if cfg.is_pressed:
        deal_pressed()
        return
    jump_state_switch()
    
def is_left_to_x2():
    if cfg.no_left_move:
        return False
    if (not cfg.auto_to_scale and is_sca()) or cfg.show_mode>=2 or cfg.left_to_x2:
        return True
    return False

ctrl_co = 0
last_small_ctrl_time=0
def ctrl2_in():    
    ll=3
    if cfg.has_show_sca:
        ll += 1
    if cfg.w2showing and len(cfg.w2snap)<=ll and cfg.show_mode!=0:
        cfg.sum='3'
        cfg.show_mode = 2
        xing()
    else:
        add_showmode()
def no_to_3():
    cfg.min_ge=0
    cfg.sum = str(scroll_len)
    cfg.show_one_on = True
    cfg.show_one_onetime = True        
    cfg.show_mode=2
    xing()
    cfg.left_to_x2 = True

def ctrl2_com(er=False):
    if er:
        clear(True)
        set_to_last(True)
    no_to_3()
def up_double():
    clear(True)
    cfg.left_sign2=0    
    cfg.can_set_acc=True
    cfg.acc_no_reset=True
def cache_gap():
    pp = get_cur_pos()
    cfg.last_m = (pp[0]-cfg.fix_pos[0],pp[1]-cfg.fix_pos[1] )          
def do_ctrl2():
    from keyr import press_no_mode
    if cfg.x2_pressed:
        return
    if cfg.x2_mode:
        x2_half_move()
    if cfg.left_sign==0:
        cfg.left_sign=1
        clear(True)
        jump_state_to_man()
        cfg.is_after_man=True
        return
    if cfg.eg_from_mid:
        up_double()
        return    
    if cfg.min_ge==3 and cfg.getokuai!=100:
        deal_pressed()
        return
    if cfg.min_ge!=0 and not cfg.min_ge_force_show and is_no_move() and cfg.show_one_on and cfg.sum=='0':
        if cfg.min_ge==leftUpGe:
            ctrl2_com(True)
            return
        
        if cfg.ge_wait_dis2!=ge_wait_dis_ctrl2:
            cfg.ge_wait_dis2=ge_wait_dis_ctrl2
        else:
            ge_force_no_show()

    elif not cfg.x2_pressed:
        if  cfg.w2showing and cfg.sum=='0' and not cfg.x2_mode:
            ctrl2_in()      
        elif not cfg.w2showing and cfg.sum=='0':
            press_no_mode(scroll_str,-1)
        else:
            from routin import zi_to_len
            zi_to_len()

def ctrl2_fire():
    global ctrl_co,last_small_ctrl_time
    last_small_ctrl_time = 0
    cfg.ctrl_l_time = 0
    cfg.ctrl_job = None
    cfg.ctrl2_job = True
    cfg.ctrl2_other = False
    cfg.ctrl2_time = time.time()

def check_ctrl2():
    global ctrl_co,last_small_ctrl_time
    from keyr import press_no_mode
    ctrl_co += 1
    if time.time() - cfg.ctrl_l_time < ctrl_double_gap and time.time()-last_small_ctrl_time>0.1 and time.time()-pm.cmd_time>0.1:   
        if True:
            press_no_mode(scroll_str,-1)
        else:
            ctrl2_fire()            
    else:
        if time.time()-cfg.ctrl_l_time<0.1:
            last_small_ctrl_time=time.time()
        cfg.ctrl_l_time = time.time()

def get_snap_sort():
    a = copy.deepcopy(cfg.w2snap)
    if len(a) == 0:
        return []
    
    a = sorted(a,key=lambda x: int(x[0]))                        
    max_idx = 0
    for i in range(len(a)):
        if is_in_screen(a[i][1], a[i][2]):
            max_idx = i
    print('max_idx', max_idx)
    return a[0:max_idx+1]

def is_no_move(cpos=None):
    if cpos==None:    
        x, y = getp(None,False)        
    else:
        x,y = cpos
    sx, sy = cfg.fix_pos    
    dx = x - sx 
    dy = y - sy            
    if round(dx) == 0 and round(dy) == 0:
        return True
    return False    

def ctrl2():
    if not cfg.w2showing:        
        return
    
    a = get_snap_sort()
    if len(a)<=0:
        return
    alast = int(a[-1][0])
    if len(cfg.add_show2)==0 or max(cfg.add_show2)<=alast:        
        cfg.x2_mode = False
        set_cfg_sum_0()
        if cfg.enable_jump_task:
            cfg.jump_task = (a[-1][1], a[-1][2])
        else:
            con_set_pos(a[-1][1], a[-1][2])
            clear(True)
        cfg.last_m = cfg.w2_last_move
        cfg.last_sum = int(a[-1][0])
        cfg.last_m_time = time.time()
        return
    mm = max(cfg.add_show2)
    aa,x,y = get_er_pos(mm)
    set_cfg_sum_0()
    if aa:
        if cfg.enable_jump_task:
            cfg.jump_task = (x,y)        
        else:
            con_set_pos(x,y)        
            clear(True)
        cfg.last_m = cfg.w2_last_move
        cfg.last_sum = mm
        cfg.last_m_time = time.time()

def xuan_get_du():
    a,b = change_sum(cfg.sum)   
    if b>=100:
        b -= 10 
    return b

def deal_xuan(mode):
    if mode==0:
        return 
    
    if is_sca():
        return
   
    if cfg.x2_mode:
        return
    
    b = xuan_get_du()
    if mode==1:
        b = -b

    x2,y2 = get_cur_pos()
    x1,y1 = cfg.fix_pos
    dx = x1-x2
    dy = y1-y2
    if round(dx)==0 and round(dy)==0:
        d = math.pi
        ll = get_sc_size()
    else:
        d = math.atan2((y1-y2),(x1-x2))
        ll = cal_dis((x1,y1),(x2,y2))
    d2 = d+b*math.pi/180
    cfg.fix_pos = ((ll*math.cos(d2))+x2,(ll*math.sin(d2))+y2 )    

def deal_e_get(dx,dy):
    a,f = change_sum(cfg.sum)
    if not a:
        f = -f
    cfg.last_sum =f

    if dx!=0 or dy!=0:
        cfg.last_m = (dx,dy)
        cfg.last_m_time = time.time()
        dx = round(dx*f + dx)
        dy = round(dy*f + dy)
        return dx,dy    
    
    dx,dy = cfg.last_m    
    cfg.last_m_time = time.time()
    dx = round(dx*f)
    dy = round(dy*f)
    return dx,dy

def deal_e(pos_check,mode=0,setLastM=True):          
    if is_ff(cfg.sum) and float(cfg.sum)<200:
        deal_xuan(mode)
        cfg.show_jiao = True
        set_cfg_sum_0()
        clear_add_show()
        return
    
    if is_ff(cfg.sum) and len(cfg.sum)>1:
        cfg.sum = cfg.sum[1:]
    while len(cfg.sum)>1 and cfg.sum[0]=='0':
        cfg.sum = cfg.sum[1:]
    x, y = getp(None, False)
    sx, sy = cfg.fix_pos
    dx = x - sx
    dy = y - sy
    if str(cfg.sum)=='0':        
        if setLastM:
            cfg.last_m = (dx,dy)
            print('set last m 5')
            # traceback.print_stack()      
            cfg.last_m_time = time.time()
            cfg.last_sum = 0
        clear(True)
        cfg.show_one_on = True
        return
    dx,dy = deal_e_get(dx,dy)        
    ddx,ddy = sx + dx, sy + dy
    cfg.fix_pos =  (ddx,ddy)        
    set_cfg_sum_0()
    clear(False)    
    if cfg.enable_jump_task:
        if pos_check:        
            cfg.jump_task = (ddx,ddy,True)
        else:        
            cfg.jump_task = (ddx,ddy,False)
    else:
        con_set_pos(ddx,ddy)
        if pos_check:
            cfg.set_fix_time = time.time()
    deal_reset_one()            

def get_xing_d():
    if '.' not in cfg.sum:
        d = round(float(cfg.sum))
        return True,d
    if '0.' == cfg.sum[0:2] and cfg.sum!='0.':
        d = -int(cfg.sum.replace('0.', '') )
        return True, d
    
    if '.' == cfg.sum[-1]:
        d = -int(cfg.sum.replace('.', '') )
        return True,d
    if not is_ff(cfg.sum):
        return False,float(cfg.sum)*get_sc_size()
    return False,-float(cfg.sum)*get_sc_size()    

def jump_state_to_man():
    jump_state_chg(1)

def xing():    
    if cfg.sum != '0':        
        isge,d = get_xing_d()
        if isge:
            deal_manu_scroll(d)
        else:
            set_scroll_len(d)        
        set_cfg_sum_0()
        return

    oriIsshow = cfg.isShow
    cfg.isShow = False
    ori = cfg.force_show
    isnmv = is_no_move()    

    if cfg.w2showing or oriIsshow:
        clear(True)
        cfg.show_one_on = False
        return

    if not cfg.show_one_on :        
        clear(True)
        cfg.show_one_on = True        
        return
    
    if not isnmv:
        if not cfg.mouse_tip_no_show or cfg.show_mouse_tip:
            cfg.show_one_on = False
        else:
            clear(True)
            cfg.show_one_on = True
        return
    
    if ori:
        clear(True)
        if cfg.mouse_tip_no_show and not cfg.show_mouse_tip:
            return
        cfg.show_one_on = False
        cfg.force_show = False
        return
    
    clear(True)
    cfg.force_show = True

def set_to_last(pos):
    x2,y2 = get_cur_pos()
    if pos:
        cfg.fix_pos = (x2-cfg.last_m[0],y2-cfg.last_m[1])
    else:
        cfg.fix_pos = (x2+cfg.last_m[0],y2+cfg.last_m[1])
    set_cfg_sum_0()
    cfg.show_one_on = False

def get_last_idx(info=None):    
    x, y = getp(None,False)        
    sx, sy = cfg.fix_pos
    dx = x - sx 
    dy = y - sy          
    if info:
        x,y,dx,dy = info
    ll = math.sqrt(dx*dx+dy*dy)
    if round(dx)==0 and round(dy)==0:
        return 0,0
    
    last_idx1 = 0
    last_idx2 = 0
    for i in range(max_l):
        if i*ll>max_l:
            break
        dstx = i*dx+x
        dsty = i*dy+y
        if is_in_cur_screen(dstx,dsty):
            last_idx1 = i
        if is_in_all_screen(dstx,dsty):
            last_idx2 = i
    return max(0,last_idx1),max(0,last_idx2)
    
def deal_13():
    from routin_scroll import is_in_pause_m,pause_cmd,deal_pause
    if cfg.sum=='0.':
        xing_kuai()        
        return
    if cfg.sum=='0.0':
        xing_kuai2()
        return
    if cfg.sum!='0' and not cfg.w2showing:
        deal_pause()
        return

    if cfg.sum!='0' and '.' not in cfg.sum and not cfg.w2showing and is_no_move():
        aa = int(float(cfg.sum))
        clear(True)
        cfg.show_one_on = True
        cfg.force_show = True      
        ge_set(aa)
        set_cfg_sum_0()
        return
    if cfg.sum!='0' and not cfg.w2showing and is_in_pause_m(cfg.sum):
        pause_cmd()
        return
    if cfg.sum!='0' and not cfg.w2showing and not cfg.show_one_on:            
        cfg.show_one_on = True
        cfg.show_one_onetime = True
        xing()
        return
    
    if cfg.sum!='0':
        xing()       
        return        
    
    if cfg.w2showing:
        ll = xing_no_effect_len
        if cfg.has_show_sca:
            ll = xing_no_effect_len+1
        if len(cfg.w2snap)<=ll:
            cfg.sum = str(xing_no_effect_add)
        else:
            cfg.sum = str('1.')
        xing()
        return
    if not cfg.w2showing and not is_no_move():
        no_to_3()        
        return
    
    ori = cfg.min_ge
    orif = cfg.force_show
    if not cfg.w2showing:        
        clear(True)
        cfg.show_one_on = True
        cfg.force_show = True                
    if orif:
        ge_add_xing(ori)

def minu_arr():    
    from routin_enter import enter_arr
    if '.' in cfg.sum:
        return
    if cfg.sum[0] not in '12346789':
        return
    a = int(cfg.sum[0])
    if len(cfg.sum)<2:
        enter_arr('',a,False,False,get_sc_size(),minu_gap_ge)
        return        
    ge = int(cfg.sum[1:])    
    enter_arr('',a,False,False,get_sc_size(),max(1,ge))

def get_win_sca(idx):
    a,b = screen_icon_size[idx]
    return a,b    

def movexy_pos2():
    if len(cfg.sum)==1:
        a=0
        b=int(cfg.sum)
    else:
        a = int(cfg.sum[0])
        b = int(cfg.sum[1])        

    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,bb,c,d =  screen_config_for_indx[idx]    
    xsca,ysca = (c-aa)/8,(d-bb)/8
    set_cfg_sum_0()
    dstx = (a-1)*xsca+aa
    if a==9:
        dstx -= screen_gap
    if a==0:
        dstx=x    
    dsty = (b-1)*ysca+bb
    if b==9:
        dsty -=screen_gap
    if b==0:
        dsty=y    
    return dstx,dsty

def movexy_pos(sm,has0=False):
    if sm:
        return movexy_pos2()
    
    xre = False
    yre = False    
    a,b = cfg.sum.split('.')
    if b=='':
        b = '0'
    va = int(float(a))
    vb = int(float(b))
    vva = va
    if has0:
        vva = va-1
    vvb = vb
    if has0:
        vvb = vb-1

    if b in '0'*10:
        b = b[:-1]+'1'
        vb = 0

    if b[0]=='0' and b[:2]!='00':
        xre = True
    if b[:2]=='00' and b[:3]!='000':
        yre = True
    if b[:3]=='000':
        xre = True
        yre = True

    x, y = get_cur_pos()
    idx = get_screen_indx(x, y)
    aa,bb,c,d =  screen_config_for_indx[idx]    
    xsca,ysca = get_win_sca(idx)
    set_cfg_sum_0()

    dstx = vva*xsca+aa
    if xre:
        dstx = c - vva*xsca

    dsty = vvb*ysca+bb
    if yre:
        dsty = d-vvb*ysca
    if has0:
        if va==0:
            dstx=x
        if vb==0:
            dsty=y

    return dstx,dsty

def movexy(sm=False):    
    dstx,dsty = movexy_pos(sm,True)
    x, y = get_cur_pos()
    if round(dstx)==x and round(dsty)==y:
        return
    dx = dstx-x
    dy = dsty-y
    du = math.atan2(-dy,-dx)
    ll = min_d*2
    clear(True)
    cfg.fix_pos = (math.cos(du)*ll+x,math.sin(du)*ll+y)
    if not cfg.w2showing:
        dotdot()
    n = math.sqrt(dx*dx+dy*dy)/ll
    n = max(0,round(n))
    if not sm:
        n = max(0,int(n))
        
    set_fix_ge_len(n)

def movexy2(sm=False):
    dstx,dsty = movexy_pos(sm)
    con_set_pos(dstx,dsty)
    clear(True)
    cfg.set_fix_time = time.time()

def ge_force_no_show():
    cfg.ge_break_dis = 0
    cfg.min_ge_force_not_show = True
    cfg.ge_wait_dis2 = max_l
    cfg.min_ge_force_show = False
    cfg.force_show = False
    
def deal_minu():        
    if cfg.sum=='0.':              
        minu_kuai()
        return
        
    if is_ff(cfg.sum) and cfg.sum[-2:]!='00':
            deal_e(True,1)
            return
    
    if '.' in cfg.sum and not cfg.w2showing and cfg.sum[-1]!='.':
        if cfg.sum[-2:]=='00':
            cfg.sum = cfg.sum[:-1]
        movexy2()
        return

    if not cfg.w2showing and cfg.sum!='0':
        minu_arr()        
        set_cfg_sum_0()
        cfg.enter_pressed_seq = []
        return

    if cfg.min_ge!=0 and not cfg.w2showing and cfg.force_show and cfg.min_ge_force_not_show:
        cfg.force_show = False
        return
    
    if cfg.min_ge!=0 and (not cfg.w2showing or len(cfg.w2snap)<=1) and not cfg.min_ge_force_not_show:
        cfg.min_ge_force_not_show = True
        cfg.ge_wait_dis2 = max_l
        cfg.min_ge_force_show = False
        return
    
    if cfg.sum=='0' and cfg.w2showing and not cfg.x2_mode:        
        ctrl2()
        cfg.click_time = time.time()
        return
    
    if cfg.sum != '0':
        deal_e(True)
        cfg.click_time = time.time()
        return
    mouse.Controller().click(mouse.Button.left)   

def show_clear():
    cfg.min_ge = 0
    cfg.w2_last_ge = 0
    cfg.w2_last_to = 0
    cfg.w2_last_dis = 0
    cfg.last_ge = 0
    cfg.w2_min_ge = 0
    cfg.to = 0

def switch_show():
    if cfg.isShow:
        cfg.isShow = False
        cfg.show_one_on = True
    else:
        cfg.isShow = True     

def do_step_move(x,y,setLast=False):            
    if setLast:
        print('set last m',x,y)
        cfg.last_m = (x,y)
        cfg.last_m_time = time.time()
    if not cfg.w2showing:
        if cfg.min_ge!=0 or cfg.force_show:
            cfg.show_one_on =True
            cfg.show_one_onetime = True
        else:    
            cfg.show_one_on = False
    mouse.Controller().move(round(x),round(y)) 

def deal_chu():
    if cfg.sum != '0':
        if len(cfg.sum)>1 and not (cfg.lock or pm.ctrl or pm.ctrl_alt):
            cfg.sum = cfg.sum[0:-1]            
        else:
            set_cfg_sum_0()    
        cfg.add_show.append(-1)
        return
    if not cfg.w2showing:
        deal_pressed()
        return
    update_du(True)
